using System;
using System.Diagnostics;
using Microsoft.WindowsAzure.StorageClient;

namespace Microsoft.WindowsAzure.Accelerator
{
    /// <summary>
    /// Extensions to the CloudBlob class, which represents a Windows Azure blob.
    /// </summary>
    public static class CloudBlobDirectoryExtensions
    {
        public static String GetChildVirtualPath(this CloudBlobDirectory directory, String uri)
        {
            //i| First remove any absolute location match in the string.
            var virtualDirectory = uri.SetPathChar('/').Substring(directory.Uri.ToString().Length).TrimStart('/','\\');
            //i| Next remove just the virtual directory relative portion in case this was a storage relative URI.
            virtualDirectory = virtualDirectory.Substring(directory.Uri.AbsolutePath.TrimStart('/').Length);
            //i| Now assume that anything left is a relative path to the CloudBlobDirectory.
            virtualDirectory = Path.Combine(directory.Uri.AbsolutePath, uri);
            Trace.TraceInformation("Directory Relative Uri:  [{0}] -> [{1}].", uri, virtualDirectory);
            return virtualDirectory;
        }
    }
}